/* -----------------------------------------------------------------------------
 * PROJECT    : lwf
 * FILE       : FractionAction.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Mar 12, 2011  mycodetalks			       Created
 */
package net.lwf.web.struts.action.maths.faction;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lwf.handler.IProcessHandler;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.handler.math.fraction.FractionComparisonHandler;
import net.lwf.handler.math.fraction.SimpleFractionHandler;
import net.lwf.web.constant.IPageTitle;
import net.lwf.web.struts.action.LwfWorksheetAction;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Class description goes here
 */
public class FractionAction extends LwfWorksheetAction {

	public ActionForward home(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		FractionActionForm faform = (FractionActionForm) form;
		faform.reset();
		return mapping.findForward("home");
	} //home
	
	public ActionForward generate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		FractionActionForm faform = (FractionActionForm) form;
		faform.setAskQuestion(true);
		
		this.generate( request, faform);
		return mapping.findForward("success");
	} //generate
	
	@Override
	public IProcessHandler getHandler(HttpServletRequest request) {
		return FractionActionHelper.getHandler(getOperation(), getGrade());
	}
	
	@Override
	public String getPdfLink() {
		return FractionActionHelper.getPdfLink(getOperation());
	}

	@Override
	public String getQuizLink() {
		return FractionActionHelper.getQuizLink(getOperation());
	}
	
	private void generate(HttpServletRequest request, FractionActionForm faform) {
		String operation = getOperation();
		if(operation.equalsIgnoreCase(IHandlerConstant.SIMPLE)) {
			this.generateSimple(request, faform);
		}
		else if(operation.equalsIgnoreCase(IHandlerConstant.COMPARISON)) {
			this.generateForComparison(request, faform);
		}
	}

	private void generateSimple(HttpServletRequest request, FractionActionForm faform) {
		//Get Handler
		SimpleFractionHandler handler = (SimpleFractionHandler) getHandler(request);
		QuestionInfo qinfo = handler.generateSingle();
		//Set the question data
		setQuestionData(request, qinfo);
		faform.setHtmlQuestion(qinfo.getHtmlQuestion());
		faform.setQuestionNumber(getQuestionNumber(request));
		faform.setInstructionText("What is fraction of the shaded area in the following picture?");
	}

	private void generateForComparison(HttpServletRequest request, FractionActionForm faform) {
		//Get Handler
		FractionComparisonHandler handler = (FractionComparisonHandler) getHandler(request);
		QuestionInfo qinfo = handler.generateSingle();
		//Set the question data
		setQuestionData(request, qinfo);
		faform.setHtmlQuestion(qinfo.getHtmlQuestion());
		faform.setQuestionNumber(getQuestionNumber(request));
		faform.setInstructionText("Identify the correct comparator from the following fractions.");
	}

	@Override
	public String getPageTitle() {
		return FractionActionHelper.getPageTitle(getOperation());
	}

	@Override
	public String getMainTitle() {
		return IPageTitle.MAIN_FRACTIONS;
	}

}



